package com.yutel.phone.xplayer.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class NetworkReceiver extends BroadcastReceiver {

	public final static String TAG = "NetworkReceiver";

	private static boolean firstConnect = true;

	@Override
	public void onReceive(Context context, Intent intent) {
		final String action = intent.getAction();

		// important! this sax driver is necessary for cling stack
		// System.setProperty("org.xml.sax.driver","org.xmlpull.v1.sax2.Driver");

		Log.d(TAG, "action = " + action);

		// to debug ethernet/wifi interface
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();

		if (activeNetInfo != null) {
			if (firstConnect) {
				firstConnect = false;
				// start airplay service
				Intent airPlay = new Intent(context, AirplayService.class);
				context.startService(airPlay);
				Log.d(TAG, "service stared");
			}
		} else {
			if (activeNetInfo == null) {
				firstConnect = true;
				// stop ariplay service
				Intent airPlay = new Intent(context, AirplayService.class);
				context.stopService(airPlay);
				Log.d(TAG, "service closed");
			}
		}

	}

}
